package com.atguigu.common.valid.validator;

import com.atguigu.common.valid.annotation.ListIntegerValue;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author: Mr.ling
 * @data: 2022/7/9
 * @apiNote
 */
public class ListIntegerValueConstraintValidator implements ConstraintValidator<ListIntegerValue,Integer> {

    private Set<Integer> set = new HashSet<>();

    /**
     * 初始化方法
     * @param constraintAnnotation annotation instance for a given constraint declaration
     */
    @Override
    public void initialize(ListIntegerValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
            set.add(val);
        }

    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        // 判断是否包含改值
        return set.contains(value);
    }
}
